package com.jk.dmpayprovider.service;


import com.jk.dmcommon.LybEntity.UserEntity;
import com.jk.dmcommon.djhEntity.DjhWatchPersonEntity;
import com.jk.dmcommon.entity.CouponEntity;
import com.jk.dmcommon.entity.OrderEntity;
import com.jk.dmcommon.entity.ShopEntity;
import com.jk.dmpayprovider.dao.OrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 文件名称:OrderService
 *
 * @Description //TODO $
 * @Date $ $
 * 作者 郭英杰
 * Copyright (c) 2021, yuxy123@gmail.com All Rights Reserved.</pre>
 */
@Service
public class OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisTemplate redisTemplate;


    //提交订单
    public void orderQuery(Integer leaveId,HttpServletRequest request,Double totalPrices,String site,Integer goodsCount,Integer couponId) {
        ShopEntity shop=  orderDao.distribution(leaveId);
        UserEntity user = (UserEntity) redisTemplate.opsForHash().get("dmLoginInfo", "loginInfo");
        Integer personId = (Integer) redisTemplate.opsForHash().get("bothPeople", "personId");
        DjhWatchPersonEntity watch=orderDao.queryPerson(personId);/*查询观演人*/

        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderId(null);
        orderEntity.setOrderId(UUID.randomUUID().toString());
        orderEntity.setOrderNum("eixhhl9647@sandbox.com");
        orderEntity.setShopEntity(shop);
        orderEntity.setShopCount(goodsCount);
        orderEntity.setShopTotal(totalPrices);
        orderEntity.setRealityShopTotal(totalPrices);
        orderEntity.setBuyUserId(user.getUserId());
        orderEntity.setBuyUserName(user.getUserName());
        if (watch==null){
            Integer userid= user.getUserId();
            DjhWatchPersonEntity watch2=orderDao.queryPerson2(userid);
            orderEntity.setContacts(watch2.getPersonName());
            orderEntity.setPapersType(watch2.getPapersType());
            orderEntity.setPapersNumber(watch2.getPapersNumber());
        }else{
            orderEntity.setContacts(watch.getPersonName());
            orderEntity.setPapersType(watch.getPapersType());
            orderEntity.setPapersNumber(watch.getPapersNumber());
        }
        orderEntity.setContactsPhone("");
        Date date = new Date();
        orderEntity.setOrderDate(date.toLocaleString());
        orderEntity.setPayDate("");
        orderEntity.setFinishDate("0");
        orderEntity.setOrderCondition(0);
        orderEntity.setCouponId(couponId);
        orderEntity.setZeroDescribe("等待支付");
        orderEntity.setOneDescribe("等待处理");
        orderEntity.setTwoDescribe("关闭订单");
        orderEntity.setThreeDescribe("订单完成,在个人中心查看");
        orderEntity.setFourDescribe("退款中");
        orderEntity.setDispatchAddress(shop.getDistrMode());
        orderEntity.setDispatchAddress(user.getUserSite());
        orderEntity.setSite(site);
        mongoTemplate.save(orderEntity);
        redisTemplate.opsForHash().put("guoqi","gq",orderEntity);
        redisTemplate.expire("guoqi",30, TimeUnit.SECONDS);
    }
    /*配送方式*/
    public  ShopEntity distribution(Integer leaveId) {
        ShopEntity shop=  orderDao.distribution(leaveId);
        return shop;
    }
    //查询优惠券
    public CouponEntity queryCoupon(Integer couponId) {
        CouponEntity coupon=orderDao.queryCoupon(couponId);
        return  coupon;
    }


  
   /* public void oedercs(HttpServletRequest request) {
        ShopEntity shopEntity = new ShopEntity();
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setId("2");
        orderEntity.setOrderId("2");
        orderEntity.setOrderNum("21314135323124");
        orderEntity.setShopEntity(shopEntity);
        orderEntity.setShopCount(2);
        orderEntity.setShopTotal(20);
        orderEntity.setRealityShopTotal(18);
        orderEntity.setBuyUserId(1);
        orderEntity.setBuyUserName("方丈");
        orderEntity.setContacts("董事长");
        orderEntity.setPapersType(1);
        orderEntity.setPapersNumber("134413218218712");
        orderEntity.setContactsPhone("124126123521");
        orderEntity.setOrderDate(new Date().toString());
        orderEntity.setPayDate(null);
        orderEntity.setFinishDate(null);
        orderEntity.setOrderCondition(0);
        orderEntity.setCouponId(1);
        orderEntity.setZeroDescribe("等待支付");
        orderEntity.setOneDescribe("等待处理");
        orderEntity.setTwoDescribe("关闭订单");
        orderEntity.setThreeDescribe("订单完成,在个人中心查看");
        orderEntity.setFourDescribe("退款中");
        orderEntity.setShippingStatus(1);
        orderEntity.setDispatchAddress("少林寺");
        orderEntity.setElecTicketNum("13456876543234765");
        request.getSession().setAttribute("goodsOrder",orderEntity);
    }*/
}
